//服务器index.js文件

const http = require('http');
const WebsocketSever = require('websocket').server;
const moment = require('moment');

//创建http服务器
const httpSever = http.createServer((req,res)=>{
  res.writeHead(404);
  res.end();
})
//创建websoclet服务器
const wsSever = new WebsocketSever({
  httpServer,
  autoAcceptConnections: true //自动接受连接
})

//监控接入
wsSever.on('connect',(connection)=>{
  //监控数据信息message
  connection.on('message',(msg)=>{
    console.log('>> message' , msg)
    if(msg.type === 'utf8'){
      var data = {
        content : '[自动回复] : 我是龙！强者就是要羞辱弱者！',
        date : moment(new Date()).format('YYYY-D-M', 'h:mm a')
      }
      connection.sendUTF( JSON.stringify(data))
    }
  })

  //监控关闭
  connection.on('close',()=>{
    console.log('socket服务关闭');
  })
})

httpSever.listen('3000',()=>{
  console.log('se')
})